import Vue from 'vue';
import GLOBAL from '@/assets/js/global'
import poiLayer from '@/components/module/poiLayer/main'
let PoiLayerConstructor = Vue.extend(poiLayer);
export function showAttribute() {
  //点击显示属性
  let editHandler = new tedp.webGis.cesium.ScreenSpaceEventHandler(
    tedp.webGis.Viewer.scene.canvas
  );
  editHandler.setInputAction((event) => {

    //兼容地下模式
    if (
      Cesium.ExpandBySmartEarth &&
      (Cesium.ExpandBySmartEarth.underEarth.enable ||
        Cesium.ExpandBySmartEarth.isStreetView)
    ) {
      return;
    }
    // 获取屏幕坐标
    let windowPosition = event.position;
    // 通过屏幕坐标获取当前位置的实体
    let pickedObject = tedp.webGis.Viewer.scene.pick(windowPosition);

    var earthPosition = tedp.webGis.Viewer.camera.pickEllipsoid(
      event.position,
      tedp.webGis.Viewer.scene.globe.ellipsoid
    );
    var cartographic = Cesium.Cartographic.fromCartesian(
      earthPosition,
      tedp.webGis.Viewer.scene.globe.ellipsoid,
      new Cesium.Cartographic()
    );
    console.log(
      `lat:${Cesium.Math.toDegrees(
        cartographic.latitude
      )},lng:${Cesium.Math.toDegrees(cartographic.longitude)}`
    );

    //处理POI点位弹窗
    if (
      pickedObject &&
      pickedObject.id &&
      pickedObject.id._description &&
      pickedObject.id._description._value &&
      pickedObject.id._description._value.write
    ) {
      if (GLOBAL.poiLayerObj) {
        GLOBAL.poiLayerObj.deleteObject()
      }
      if (GLOBAL.poiEntity) {
        GLOBAL.poiEntity.billboard.scale._value = 0.25;
      }
      GLOBAL.poiEntity = pickedObject.id;
      pickedObject.id.billboard.scale._value = 0.38;
      let instance = new PoiLayerConstructor({
        data: {
          list: pickedObject.id._description._value.write
        }
      });
      instance.$mount();
      GLOBAL.poiLayerObj = tedp.webGis.Creator.createDivPoint('', {
        lon: pickedObject.id._description._value.x,
        lat: pickedObject.id._description._value.y
      }, {
        type: "custom",
        offset: ["c", 70],
        description: instance.$el,
        near: 0,
        far: 300000
      });
      setTimeout(() => {
        let distance = tedp.webGis.Core.getPointToCameraDistance(Viewer, pickedObject.id.position._value)
        Viewer.flyTo(pickedObject.id, {
          offset: {
            heading: tedp.webGis.Viewer.camera.heading,
            pitch: tedp.webGis.Viewer.camera.pitch,
            range: distance
          },
          duration: 1
        });
      }, 100)


    }

  }, tedp.webGis.cesium.ScreenSpaceEventType.LEFT_DOWN);
}

export function createPoi(attr) {
  if (!attr.x || !attr.y) return;
  let _obj = tedp.webGis.Viewer.entities.add({
    position: tedp.webGis.cesium.Cartesian3.fromDegrees(
      attr.x,
      attr.y,
      40
    ),
    description: attr,
    billboard: {
      scale: 0.25,
      image: SmartEarthRootUrl + attr.icon, // default: undefined
    },
  });
  GLOBAL.poiContainer[attr.type].push(_obj.id);
  return _obj.id
}

export function delPoi(type) {
  if (GLOBAL.poiContainer[type]) {
    GLOBAL.poiContainer[type].forEach(function (obj) {
      let entity = tedp.webGis.Viewer.entities.getById(obj);
      tedp.webGis.Viewer.entities.remove(entity);
    });
    GLOBAL.poiContainer[type] = null
  }
}

//创建火灾预警点
export function addFire() {
  //添加火焰
  let position = {
    x: 120.51801094165354,
    y: 30.604078269915963,
    z: 20,
  }
  GLOBAL.fireObj = tedp.webGis.Analysis.createParticleEffect("flame", position, {
    translation: tedp.webGis.cesium.Cartesian3.fromElements(0, 0, 0), //平移
  },
    function (data) {
      InitialOrientation();
    }
  );
}



export function InitialOrientation() {
  var object1 = {
    value: {
      destination: tedp.webGis.cesium.Cartesian3.fromDegrees(120.52083537153082,
        30.60701237559848,
        338.674591454659),
      orientation: {
        heading: 3.7339704373224287,
        pitch: -0.5854698439800252,
        roll: 6.2831750634326236
      },
      duration: 3
    }
  };
  tedp.webGis.Viewer.camera.flyTo(object1.value);
}

//视角跳转
export function ViewJump() {
  var object1 = {
    value: {
      destination: tedp.webGis.cesium.Cartesian3.fromDegrees(120.51823932804594,
        30.603398671473347,
        690.5615011432435),
      orientation: {
        heading: 3.083948776801179,
        pitch: -1.552908561265717,
        roll: 0
      },
      duration: 3
    }
  };
  tedp.webGis.Viewer.camera.flyTo(object1.value);
}

//计算两点之间的距离
export function TwoPointsDistance(pos1, pos2) {
  var satrt = Cesium.Cartographic.fromDegrees(pos1.x, pos1.y, 0)

  var end = Cesium.Cartographic.fromDegrees(pos2.x, pos2.y, 0)

  var geodesic = new tedp.webGis.cesium.EllipsoidGeodesic();

  geodesic.setEndPoints(satrt, end);

  var distance = geodesic.surfaceDistance
  return { distance, geodesic }
}

//计算最优路径
export function optimalRoute(pos1, pos2) {
  var positionList = [];
  //var routeUrl = "http://183.162.245.49:3301/gisserver/wnsserver/txline_wns?start=120.51746397665%2C30.602484979352&end=120.51816874039%2C30.601830555877&featureType=&propertyName=Shape&tactic=&tolerance=10&request=FindRoute&format=json";
  var routeUrl = "http://183.162.245.49:3301/gisserver/wnsserver/txline_wns?start=" + pos1.x + "," + pos1.y + "&end=" + pos2.x + "," + pos2.y + "&propertyName=Shape&tolerance=10&request=FindRoute&format=json";
  $.ajax({
    url: routeUrl,
    type: 'GET',
    dataType: "json",
    success: data => {
      for (var i = 0; i < data.features.length; i++) {
        if (data.features[i].features) {
          for (var j = 0; j < data.features[i].features.length; j++) {
            var lng1 = data.features[i].features[j].geometry.coordinates[0][0];
            var lat1 = data.features[i].features[j].geometry.coordinates[0][1];
            var lng2 = data.features[i].features[j].geometry.coordinates[1][0];
            var lat2 = data.features[i].features[j].geometry.coordinates[1][1];
            positionList.push(lng1, lat1, lng2, lat2);
          }

        } else {
          var lng = data.features[i].geometry.coordinates[0];
          var lat = data.features[i].geometry.coordinates[1];
          positionList.push(lng, lat);
        }

      }
      GLOBAL.line = tedp.webGis.Viewer.entities.add({
        name: "line on terrain",
        polyline: {
          positions: tedp.webGis.cesium.Cartesian3.fromDegreesArray(positionList),
          width: 30,
          material: new tedp.webGis.cesium.PolylineGlowMaterialProperty({
            glowPower: 0.1,
            color: tedp.webGis.cesium.Color.ORANGE.withAlpha(0.9),
          }),
          clampToGround: true,
        },
      });
    }
  });
}
//清除最优路径线
export function removeLine() {
  tedp.webGis.Viewer.entities.removeById(GLOBAL.line.id);
}

//创建道路线
export function createPathLayer() {
  let pathLayer = tedp.webGis.Creator.createPathLayer({
    url: 'http://183.162.245.49:3301/gisserver/wfsserver/txline_wfs?version=1.3.0&request=GetFeature&typename=TXLW84&propertyname=*&bbox=60.32108306,2.692189925,148.21170806,57.184377425&format=json',
    color: "#0033FF", //线的颜色
    width: 5.0, //线的宽度
    pointColor: "#FFFFFF", //移动点的颜色
    speed: 5,
    far: 50000
  });
}

//对模型进行裁切
let ModelClipping
export function clippingModel(isInner,model) {
  remove1();
     // let positions = entity.polygon.hierarchy.getValue().positions;
      let positions = tedp.webGis.cesium.Cartesian3.fromDegreesArray([


        120.51814120192394,
        30.604699017835028,
        120.51814120192394,
        30.60276013130276,
        120.52090060023522,
        30.60276013130276,
        120.52090060023522,
        30.604699017835028




        // 120.51812491875206,
        // 30.60462422031599,
        // 120.51812491875206,
        // 30.60284315099392,
        // 120.52101633482272,
        // 30.60284315099392,
        // 120.52101633482272,
        // 30.60462422031599,
      ])

      ModelClipping = tedp.webGis.Creator.createModelClipping(
        model.item, {
        type: 'polygon',
        positions,
        isInner
      });
};

export function remove1() {
  ModelClipping && ModelClipping.remove()
}




